package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperHotSearch;
import com.ruoyi.system.service.ISuperHotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 热门搜索,存储热搜关键词及热度Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/search/searchhot")
public class SuperHotSearchController extends BaseController
{
    @Autowired
    private ISuperHotSearchService superHotSearchService;

    /**
     * 用户搜索行为上报
     * 如果搜索同一关键词超过5次则加入热门搜索
     */
    @PostMapping("/report")
    public AjaxResult report(@RequestBody Map<String, String> body) {
        String keyword = body.get("keyword");
        superHotSearchService.reportSearch(keyword);
        return success();
    }

    /**
     * 点击热门搜索（score +1）
     */
    @PostMapping("/click")
    public AjaxResult click(@RequestBody Map<String, String> body) {
        String keyword = body.get("keyword");
        superHotSearchService.increaseHotScore(keyword);
        return success();
    }
    /**
     * 查询热门搜索,存储热搜关键词及热度列表
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperHotSearch superHotSearch)
    {
        startPage();
        List<SuperHotSearch> list = superHotSearchService.selectSuperHotSearchList(superHotSearch);
        return getDataTable(list);
    }

    /**
     * 导出热门搜索,存储热搜关键词及热度列表
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:export')")
    @Log(title = "热门搜索,存储热搜关键词及热度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperHotSearch superHotSearch)
    {
        List<SuperHotSearch> list = superHotSearchService.selectSuperHotSearchList(superHotSearch);
        ExcelUtil<SuperHotSearch> util = new ExcelUtil<SuperHotSearch>(SuperHotSearch.class);
        util.exportExcel(response, list, "热门搜索,存储热搜关键词及热度数据");
    }

    /**
     * 获取热门搜索,存储热搜关键词及热度详细信息
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superHotSearchService.selectSuperHotSearchById(id));
    }

    /**
     * 新增热门搜索,存储热搜关键词及热度
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:add')")
    @Log(title = "热门搜索,存储热搜关键词及热度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperHotSearch superHotSearch)
    {
        return toAjax(superHotSearchService.insertSuperHotSearch(superHotSearch));
    }

    /**
     * 修改热门搜索,存储热搜关键词及热度
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:edit')")
    @Log(title = "热门搜索,存储热搜关键词及热度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperHotSearch superHotSearch)
    {
        return toAjax(superHotSearchService.updateSuperHotSearch(superHotSearch));
    }

    /**
     * 删除热门搜索,存储热搜关键词及热度
     */
    @PreAuthorize("@ss.hasPermi('search:searchhot:remove')")
    @Log(title = "热门搜索,存储热搜关键词及热度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superHotSearchService.deleteSuperHotSearchByIds(ids));
    }
}